<?php
namespace Guider\Controller;
use Think\Controller;

class CustomizedGuiderController extends \Org\Util\AdminController {

	/**
	 * model的中文名称
	 */
	protected $modelName = '定制订单';

	/**
	 * model的icon
	 */
	protected $modelIcon = 'icon-star';

	/**
	 * 权限验证标志位,默认false
	 * false:所有action都需要权限验证;
	 * true:所有action都不需要权限验证;
	 * array:array中的action不需要权限验证，其余需要。
	 */
	protected $notAuthority = false;

	/**
	 * 创建之后做的事情
	 */
	public function interfaceAfterCreate($id) {
	}

	/**
	 * 删除之前做的事情
	 */
	public function interfaceBeforeDelete($id) {
	}

	/**
	 * 更新之前做的事情
	 */
	public function interfaceBeforeUpdate($id) {
		if (IS_POST) {
			$customizedGuider = D('CustomizedGuider')->find($id);
			//导游已经处理完成
			if (($customizedGuider['state'] == '待导游处理') && ($_POST['content'] != '')) {
				D('CustomizedGuider')->where(['id' => $id])->data(['state' => '待签署合同'])->save();
			}
			//如果合同内容有修改，修改买方为未签署
			if (($customizedGuider['guider_contract'] != $_POST['guider_contract']) || ($customizedGuider['content'] != $_POST['content'])) {
				$_POST['user_contract_state'] = '未签署';
				// $_POST['guider_contract_state'] = '未签署';
				$_POST['state'] = '待签署合同';
			}
			//第一次签署合同
			if (($_POST['guider_contract_state'] == '已签署') && ($customizedGuider['guider_contract_state'] == '未签署')) {
				D('CustomizedGuider')->where(['id' => $id])->data(['guider_contract_time' => time()])->save();
				//用户已经签署并且订单状态是待签署合同
				if (($customizedGuider['user_contract_state'] == '已签署') && ($customizedGuider['state'] == '待签署合同')) {
					D('CustomizedGuider')->where(['id' => $id])->data(['state' => '待支付'])->save();
				}
			}

		}
	}

	/**
	 * 父类中已经有典型的CRUD操作并预留入口
	 * 如有较大改动则覆盖一下
	 */
	public function create() {
		parent::create();
	}

	public function delete() {
		parent::delete();
	}

	public function update($id) {
		parent::update($id);
	}

	public function list() {
		//处理简介ID
		$guider = D('UserGuider')->where(['principal_id' => session('导游')['id']])->find();
		if ($guider['h5_id'] == 0) {
			//导游H5简介ID需要获取
			$h5Info = file_get_contents("http://h5.qq1000.cn/Home/scene/add_create");
			$h5Info = json_decode($h5Info);
			//更新主表h5_id
			D('UserGuider')->where(['principal_id' => session('导游')['id']])->data(['h5_id' => $h5Info->obj])->save();
		}
		$_GET['searchMap']['guider_user_id'] = session('导游')['id'];
		parent::list();

	}

	public function tableOpeationButton() {
		return '
			<a href="__ROOT__/Home/Index/call/id1/' . session('导游')['id'] . '/id2/{$vo[\'user_id\']}">
                <button class="btn btn-xs btn-success" style="border: 1px;">
                    <i class="icon-phone" style="margin-right: 0px;"></i>
                </button>
            </a>
            <a href="__MODULE__/ContractOrder/list?searchMap[order_id]]={$vo[\'id\']}&searchMap[order_type]]=customized_guider">
                <button class="btn btn-xs btn-success" style="border: 1px;">
                    <i class="icon-file" style="margin-right: 0px;"></i>
                </button>
            </a>';
	}

	public function wapTableOpeationButton() {
		return '
											<li>
                                                <a href="__ROOT__/Home/Index/call/id1/' . session('导游')['id'] . '/id2/{$vo[\'user_id\']}" class="tooltip-success" data-rel="tooltip" title="Edit">
                                                    <span class="green">
                                                        <i class="icon-phone bigger-120"></i>
                                                    </span>
                                                </a>
                                            </li>';
	}

	public function detail($id = 0) {
		parent::detail($id);
	}

	public function import() {
		parent::import();
	}

	public function export() {
		parent::export();
	}
}